/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import data.Data;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import data.Data;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Riso-Desktop
 */
@WebServlet(name = "zapisDoDB", urlPatterns = {"/zapisDoDB"})
public class zapisDoDB extends HttpServlet {

    Data data;
    HttpSession session;
    String driver = "com.mysql.jdbc.Driver";
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    String id = "0";

    @Override
    public void init() throws ServletException {
        pripojenie();
    }

    public void pripojenie() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + "portalpredeti", "root", "");
        } catch (Exception ex) {
        }
    }

    private synchronized void zapis() {
        try {
            stmt = con.createStatement();
            stmt.executeUpdate("update user set udaje = '" + data.string1 + "' where id=" + id);
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private synchronized void posli(HttpServletResponse response) {

        try {
            OutputStream outstr = response.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outstr);
            oos.writeObject(id);
            oos.flush();
            oos.close();
        } catch (Exception e) {
        }
    }

    private synchronized void poslinacitane(HttpServletResponse response, HttpServletRequest request) {

        try {
            InputStream in = request.getInputStream();
            ObjectInputStream inputFromApplet = new ObjectInputStream(in);
            id = (String) inputFromApplet.readObject();
            data = (Data) inputFromApplet.readObject();
            inputFromApplet.close();

            OutputStream outstr = response.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outstr);
            oos.writeObject(zistiCijeUlozene());
            oos.flush();
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private synchronized String zistiCijeUlozene() {
        String d = null;
        try {

            stmt = con.createStatement();
            rs = stmt.executeQuery("select udaje from user where id=" + id + "");
            rs.next();
            d = rs.getString("udaje");
        } catch (Exception e) {
            d = e.toString() + ":" + id;
        }
        if (d != null) {
            return d;
        } else {
            return d = "nic";
        }
    }

    private synchronized String co(HttpServletRequest request) {
        String s = null;
        try {

            InputStream in = request.getInputStream();
            ObjectInputStream inputFromApplet = new ObjectInputStream(in);
            s = (String) inputFromApplet.readObject();
            inputFromApplet.close();

        } catch (Exception e) {
        }
        return s;
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/x-java-serialized-object");
        //PrintWriter out = response.getWriter();
        session = request.getSession();
        try {
            InputStream in = request.getInputStream();
            ObjectInputStream inputFromApplet = new ObjectInputStream(in);
            String akcia = (String) inputFromApplet.readObject();
            //kontrola akcie
            if (akcia != null) {
                if (akcia.equals("nacitat")) {
                    id = (String) inputFromApplet.readObject();
                    data = (Data) inputFromApplet.readObject();
                    inputFromApplet.close();

                    OutputStream outstr = response.getOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(outstr);
                    oos.writeObject(zistiCijeUlozene());
                    oos.flush();
                    oos.close();
                } else if (akcia.equals("zapis")) {
                    id = (String) inputFromApplet.readObject();
                    data = (Data) inputFromApplet.readObject();
                    zapis();
                    inputFromApplet.close();

                    posli(response);
                }





            }

            session.setAttribute("ID", id);
        } catch (Exception e) {
        } finally {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
